package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.member.dao.IMemberUserTagDao;
import com.edou.member.dto.MemberUserTagCondition;
import com.edou.member.entity.MemberUserTag;
import com.edou.member.service.IMemberUserTagService;

/**
 * 会员标签关联表
 * @creator     
 * @create-time 2016/8/8 星期一 9:39:03
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberUserTagServiceImpl")
@Transactional(value = "transactionManager")
public class MemberUserTagServiceImpl implements IMemberUserTagService{

	@Resource(name = "memberUserTagDaoImpl")
	private IMemberUserTagDao memberUserTagDao;

	/**
	 * 查询所有会员标签关联表
	 */
	public List<MemberUserTag> listMemberUserTagAll(){
		return memberUserTagDao.listMemberUserTagAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberUserTagByPage(PageResult<MemberUserTag> pageResult,MemberUserTagCondition condition){
		int rows = memberUserTagDao.listMemberUserTagByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberUserTag> list = memberUserTagDao.listMemberUserTagByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员标签关联表
	 */
	public MemberUserTag getMemberUserTagById(Long id){
		return memberUserTagDao.getMemberUserTagById(id);
	}

	/**
	 * 新增
	 * @param item 会员标签关联表
	 */
	public void add(MemberUserTag item){
		memberUserTagDao.add(item);
	}

	/**
	 * 修改
	 * @param item 会员标签关联表
	 */
	public void update(MemberUserTag item){
		memberUserTagDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(Long[] ids){
		memberUserTagDao.delByIds(ids);
	}
	

	/**
	 * 用户批量添加自己关注的标签
	 * @param ids ID集合
	 */
	public void memberUserCareTag(String memberUserId,String tagIds){
		if(StringUtils.isBlank(tagIds)){
			return;
		}
		
		//将原来的用户标签删除后重新添加
		this.delTagByMemberUserId(memberUserId);
		//将不会变化的元素填加进去
		MemberUserTag memberUserTag = new MemberUserTag();
		memberUserTag.setMemberUserId(memberUserId);
		memberUserTag.setType(0);
		//循环添加
		String[] ids = tagIds.split(",");
		for (String item : ids) {
			memberUserTag.setMemberTagId(Long.parseLong(item));
			this.add(memberUserTag);
		}
	}
	
	/**
	 * 根据memberID批量删除
	 * @param ids ID集合
	 */
	@Override
	public void delTagByMemberUserId(String memberUserId) {
		memberUserTagDao.delTagByMemberUserId(memberUserId);
	}

	/**
	 * 根据用户获取其相关ID
	 * @param memberUserId
	 */
	@Override
	public List<MemberUserTag> listUserTagByUserId(String memberUserId) {
		return memberUserTagDao.listUserTagByUserId(memberUserId);
	}

}

